<template>
  <!-- 树形子组件  用了一个行列布局-->
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%;"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown trigger="click" @command="handleCommand">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <!-- isRoot默认值为false  取反为true的时候才显示编辑部门和删除部门 -->
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
              <!-- <el-dropdown-item v-if="!isRoot" @click.native="delEvent">删除部门</el-dropdown-item> -->
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>

</template>

<script>
import { reqDelDepartMents } from '@/api/departments'
export default {
  props: {
    nodeData: {
      required: true,
      type: Object
    },
    // 是否是根节点
    isRoot: {
      type: Boolean,
      default: false // 默认值
    }
  },
  methods: {
    // command	点击菜单项触发的事件回调	dropdown-item 的指令
    handleCommand(command) {
      // console.log(command)
      if (command === 'add') {
        // console.log('add')
        this.$emit('add-depts', this.nodeData) // 通知父打开弹框,将nodeData数据传递过去
      } else if (command === 'edit') {
        console.log('edit')
        this.$emit('edit-depts', this.nodeData)
      } else {
        // console.log('del')
        // console.log(this.nodeData)
        this.$confirm(`此操作将永久删除部门${this.nodeData.name}, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await reqDelDepartMents(this.nodeData.id) // 删除接口 传入api
          this.$message.success('删除成功') // 提示
          this.$emit('del-depts') // 通知父亲 重新渲染列表
        }).catch(() => {
          // console.log('取消', error)
        })
      }
    }
    // delEvent() {
    //   console.log(123)
    // }
  }
}
</script>

<style>

</style>
